const http = require('../../../utils/http.js')
const Base64 = require('../../../utils/base64.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        navBar: {
            title: '我的余额',
            btn: 'goBack'
        },
        balance: [],
        chosen: '0'
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 获取输入的金额
     */
    inputTxt(e) {
        this.setData({
            money: e.detail.value,
            chosen: '0'
        })
    },

    /**
     * 选择充值金额
     */
    chooseMoney(e) {
        let id = e.currentTarget.dataset.id,
            amount = e.currentTarget.dataset.money.replace("元", "")
        this.setData({
            chosen: id,
            money: amount,
            amount: amount
        })
    },

    /**
     * 获取我的余额
     */
    getBalance(token) {
        let that = this
        let url = 'Ucenter_userMoney'

        http._post(
            url,
            { token: token },
            res => {
                console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        balance: data.data
                    })
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 获取充值列表
     */
    getRechargeList() {
        let that = this
        let url = 'Ucenter_rechargeList'

        http._post(
            url,
            {},
            res => {
                console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    that.setData({
                        rechargeList: data.data
                    })
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 充值
     */
    recharge() {
        let that = this,
            url = 'Ucenter_recharge',
            token = wx.getStorageSync('token'),
            rid = that.data.chosen,
            amount = that.data.chosen === '0' ? that.data.money : that.data.amount

        if (!amount) {
            wx.showToast({
                title: '请选择或者输入充值金额',
                icon: 'none',
                duration: 3000
            })

            return false
        }


        wx.login({
            success: function (res) {
                let code = res.code
                // console.log(code)

                http._post(
                    url,
                    { token: token, rid: rid, amount: Base64.CusBASE64.encoder('UNO' + amount + 'HACHA'), type: 4, code: code },
                    res => {
                        // console.log(res.data)

                        let data = res.data
                        if (data.re === '1') {
                            let pay_config = data.data.rinfo.jsApiParameters
                            wx.requestPayment({
                                appId: pay_config.appId,
                                timeStamp: pay_config.timeStamp,
                                nonceStr: pay_config.nonceStr,
                                // package: 'prepay_id=' + pay_config.prepayid,
                                package: pay_config.package,
                                signType: pay_config.signType,
                                paySign: pay_config.paySign,
                                success: function (res) {
                                    wx.navigateTo({
                                        url: '/pages/account/balance/balanceSuc/balanceSuc',
                                    })
                                },
                                fail: function (res) {
                                    wx.showToast({
                                        title: res.err_desc,
                                        icon: 'none',
                                        duration: 3000
                                    })
                                }
                            })
                        } else {
                            wx.showToast({
                                title: data.info,
                                icon: 'none',
                                duration: 3000
                            })
                        }
                    },
                    err => {
                        console.log(err)
                    }
                )
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const token = wx.getStorageSync('token')

        this.getBalance(token)  // 获取余额
        this.getRechargeList()  // 获取充值列表
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})